<?php

/**
 * 微博抓取管理
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");

include_once($_SERVER["DOCUMENT_ROOT"] . "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/mblog/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/tuijian/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/qq/inc.php");
$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
		//页码
	cgi::get($g_cgival["p"], "p", 1);
	$g_cgival["p"] = intval($g_cgival["p"]);

	cgi::get($g_cgival["type"], "type", 0);
	$g_cgival["type"] = intval($g_cgival["type"]);
	
	cgi::both($g_cgival["tag_id"], "tag_id", "");
	$g_cgival["tag_id"] = string::un_html($g_cgival["tag_id"]);
	
	cgi::both($g_cgival["mids"], "mids", "");

}

function get_data()
{
	global $g_cgival, $g_pro, $g_show,$config;
	$user_info = new user_info();
	$mids  = join(",",$g_cgival["mids"]);
	$mblog_from_api = new mblog_from_api();
	$db_mblog_pic_content = new db_mblog_pic_content();
	$mblog_list = $mblog_from_api->api_get_mblog_by_mids($mids,$trim_user=0);
	$tag_id = $g_cgival["tag_id"];
	
	$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
	$tag_item = $db_mblog_cron_topic_tag->admin_get_mblog_cron_topic_tag_item_by_id($tag_id);
	$topic_id = $tag_item["topic_id"];
	$site_name = $config["site_name"][$topic_id];
	$tag_name = $tag_item["tag_name"];;
	
	
	foreach($mblog_list["data"]["result"] as $value1)
	{
			if(!$value1["user"]["id"])
			{
				continue;
			}
			$value1["timestamp"] = strtotime($value1["created_at"]);
			$u_time = $value1["timestamp"];
			if($max_time < $u_time) $max_time = $u_time;
			if($min_time > $u_time || !$min_time)$min_time=$u_time;
			$create_time = date("Y-m-d H:i:s",$u_time);
			$pic_url=$value1["bmiddle_pic"];
			
			$md5_content = md5($value1["text"]);
			$date =  date("Y-m-d",$u_time);
			$content = $value1["text"];
			$source =  $value1["from"];
			$nickname = $value1["user"]["name"];
			$qq_user_info = $user_info->get_user_info_by_sina_uid($value1["user"]["id"]);
			$uid = $qq_user_info["uid"];
			$mid = $value1["mid"];
			$head_url = $value1["user"]["profile_image_url"];
			$love_count = $value1["reposts_count"] + $value1["comments_count"] ;
			
			$from = "2";
			$db_mblog_pic_content->admin_insert_mblog_pic_content($content,$topic_id,$site_name,$pic_url,$pic_width_pubu,$pic_height_pubu,$source,$show_type,$from,
$uid,$nickname,$date,$mid,$create_time,$count_love,$head_url,$tag_id,$tag_name);
			
			$g_show["uid"] = $uid;
	}

	
			$user_info = new user_info();
			$user_info->update_user_mblog_count_and_main_topic($uid,$qq_user_info);
}

$g_show["error"] = 0;
$g_show["errmsg"] = '';
try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album_list.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	
	echo output::json("json",$g_show);
	//echo json_encode($g_show);
}

